package com.banzhi.shop.entity.NewLogin;

import com.banzhi.shop.entity.Login.Login;
import com.banzhi.shop.model.CallBack.LoginModelCallBack;
import com.banzhi.shop.model.ILoginModel;
import com.banzhi.shop.state.URLString;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import static com.banzhi.shop.state.URLString.verifyLoginUrl;


/**
 * Created by HT on 2017/5/3.
 * 登录
 */
public class LoginModelImpl implements ILoginModel {

    @Override
    public void login(String username, String password, final LoginModelCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.LoginUrl);
        params.addBodyParameter("mobile", username);
        params.addBodyParameter("pwd", password);
        LogUtils.e("login", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        Login login = gson.fromJson(result, Login.class);
                        callBack.onloginSuccess(login.getRes());
                    }else {
                        callBack.onloginError( jsonobject );
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("Login", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(1)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }

    @Override
    public void WXlogin(String openid_wx, final LoginModelCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.weiXinLoginUrl);
        params.addBodyParameter("openid_wx", openid_wx);
        LogUtils.d("WXlogin", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        Login login = gson.fromJson(result, Login.class);
                        callBack.onWXloginSuccess(login.getRes());
                    } else if (jsonobject.optString("code").equals("1")){
                        callBack.onWXloginError(jsonobject);
                    }else if (jsonobject.optString("code").equals("2")) {
                        Gson gson = new Gson();
                        Login login = gson.fromJson(result, Login.class);
                        callBack.onBindingPhoneSuccess( login.getRes());
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("WXlogin", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(31)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }

    @Override
    public void YZMlogin(String mobile, String verify,final LoginModelCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + verifyLoginUrl);

        params.addBodyParameter("mobile", mobile);
        params.addBodyParameter("verify", verify);
        LogUtils.d("YZMlogin", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        Login login = gson.fromJson(result, Login.class);
                        callBack.onloginSuccess(login.getRes());

                    }else {
                        callBack.onloginError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("YZMlogin", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(55)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }



    //绑定手机号
    @Override
    public void BindingLogin(String openid_wx,String mobile, String verify, final LoginModelCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.bindingMobileUrl);
        params.addBodyParameter("openid_wx", openid_wx);
        params.addBodyParameter("mobile", mobile);
        params.addBodyParameter("verify", verify);
        x.http().post( params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonObject = new JSONObject( result );
                    if (jsonObject.optString( "code" ).equals( "0" )) {
                        Gson gson = new Gson();
                        Login login = gson.fromJson(result, Login.class);
                        callBack.onBindingLoginSuccess(login.getRes());
                    } else {
                        callBack.onBindingLoginError( jsonObject );
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable throwable, boolean b) {
                LogUtils.e("BindingLogin", "---onError----" +throwable.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(30)");

            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();

            }
        } );

    }
}
